﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public Rigidbody2D rb;
    public Animator animator;
    public float speed = 5f;

    Vector2 moveMent;

    void Start()
    {

    }
    void Update()
    {
        GetInput();
        SwitchAnim();
    }

    void FixedUpdate()
    {
        Move();
    }

    void GetInput()
    {
        if (moveMent.y == 0) moveMent.x = Input.GetAxisRaw("Horizontal");
        if (moveMent.x == 0) moveMent.y = Input.GetAxisRaw("Vertical");
    }

    void SwitchAnim()
    {
        animator.SetFloat("Horizontal",moveMent.x);
        animator.SetFloat("Vertical", moveMent.y);
        animator.SetFloat("Speed", moveMent.sqrMagnitude);
        if (moveMent.sqrMagnitude != 0)
        {
            animator.SetFloat("Face", moveMent.x * 2 + moveMent.y);
        }
    }

    void Move()
    {
        rb.MovePosition(rb.position + moveMent * speed * Time.fixedDeltaTime);
    }
}
